package com.gqz.equip.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.poi.ExcelUtil;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.TEnWzEquipEntity;
import com.gqz.equip.app.mapper.TEnWzEquipMapper;
import com.gqz.equip.entity.TEnWzCrkOrderEntity;
import com.gqz.equip.entity.TEnWzCrkOrderEquipEntity;
import com.gqz.equip.mapper.TEnWzCrkOrderEquipMapper;
import com.gqz.equip.mapstruct.TEnWzCrkOrderEquipMapstruct;
import com.gqz.equip.service.ITEnWzCrkOrderEquipService;
import com.gqz.equip.vo.EnWzCrkOrderEquipExportVo;
import com.gqz.equip.vo.TEnWzCrkOrderEquipVo;
import com.gqz.equip.vo.TEnWzCrkOrderEquipXqVo;
import com.gqz.equip.vo.TEnWzCrkOrderVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 新能源-物资-出入库工单设备表 业务逻辑实现层ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class TEnWzCrkOrderEquipServiceImpl extends ServiceImpl<TEnWzCrkOrderEquipMapper, TEnWzCrkOrderEquipEntity> implements ITEnWzCrkOrderEquipService {

    @Qualifier("TEnWzCrkOrderEquipMapstructImpl")
    @Autowired
    private TEnWzCrkOrderEquipMapstruct mapstruct;
    @Resource
    private TEnWzEquipMapper tEnWzEquipMapper;

    @Override
    public List<TEnWzCrkOrderEquipVo> getByDjbh(String djbh) {
        MPJLambdaWrapper<TEnWzEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzEquipEntity.class);
        queryWrapper.select(TEnWzCrkOrderEquipEntity::getId);
        queryWrapper.select(TEnWzCrkOrderEquipEntity::getDjbm);
        queryWrapper.innerJoin(TEnWzCrkOrderEquipEntity.class, TEnWzCrkOrderEquipEntity::getSbbm, TEnWzEquipEntity::getSbbm);
        queryWrapper.eq(TEnWzEquipEntity::getSfsc, 0);
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getDjbm, djbh);
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getSfsc, 0);
        List<TEnWzCrkOrderEquipVo> entities = tEnWzEquipMapper.selectJoinList(TEnWzCrkOrderEquipVo.class, queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return entities;
        }
        return new ArrayList<>();
    }

    @Override
    public Page<TEnWzCrkOrderEquipVo> page(RequestForm<TEnWzCrkOrderEquipVo> requestForm) {
        TEnWzCrkOrderEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnWzCrkOrderEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TEnWzCrkOrderEquipEntity.class);
        Page<TEnWzCrkOrderEquipEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnWzCrkOrderEquipEntity> iPage = baseMapper.selectJoinPage(page, TEnWzCrkOrderEquipEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<TEnWzCrkOrderEquipVo> requestForm) {
        TEnWzCrkOrderEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnWzCrkOrderEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(TEnWzCrkOrderEquipEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<TEnWzCrkOrderEquipVo> vos) {
        List<TEnWzCrkOrderEquipEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnWzCrkOrderEquipEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<TEnWzCrkOrderEquipVo> vos) {
        List<TEnWzCrkOrderEquipEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnWzCrkOrderEquipEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


    @Override
    public Page<TEnWzCrkOrderEquipVo> getByDjBhPage(RequestForm<TEnWzCrkOrderVo> requestForm) {
        TEnWzCrkOrderVo wzCrkOrderVo = requestForm.getEntity();
        MPJLambdaWrapper<TEnWzEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzEquipEntity.class);
        queryWrapper.select(TEnWzCrkOrderEquipEntity::getId);
        queryWrapper.select(TEnWzCrkOrderEquipEntity::getDjbm);
        queryWrapper.innerJoin(TEnWzCrkOrderEquipEntity.class, TEnWzCrkOrderEquipEntity::getSbbm, TEnWzEquipEntity::getSbbm);
        queryWrapper.eq(TEnWzEquipEntity::getSfsc, 0);
        queryWrapper.eq(StrUtil.isNotBlank(wzCrkOrderVo.getId()), TEnWzCrkOrderEquipEntity::getDjbm, requestForm.getEntity().getId());
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getSfsc, 0);
        if (requestForm.getEntity().getType() != null && !"".equals(requestForm.getEntity().getType())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getType, requestForm.getEntity().getType());
        }

        queryWrapper.eq(StrUtil.isNotBlank(wzCrkOrderVo.getSbbm()), TEnWzEquipEntity::getSbbm, wzCrkOrderVo.getSbbm());
        queryWrapper.like(StrUtil.isNotBlank(wzCrkOrderVo.getSbmc()), TEnWzEquipEntity::getSbmc, wzCrkOrderVo.getSbmc());
        queryWrapper.eq(StrUtil.isNotBlank(wzCrkOrderVo.getYxzt()), TEnWzEquipEntity::getYxzt, wzCrkOrderVo.getYxzt());
        queryWrapper.like(StrUtil.isNotBlank(wzCrkOrderVo.getCcbm()), TEnWzEquipEntity::getCcbm, wzCrkOrderVo.getCcbm());
        queryWrapper.eq(StrUtil.isNotBlank(wzCrkOrderVo.getSbztbm()), TEnWzEquipEntity::getSbztbm, wzCrkOrderVo.getSbztbm());
        queryWrapper.eq(StrUtil.isNotBlank(wzCrkOrderVo.getSblxbm()), TEnWzEquipEntity::getSblxbm, wzCrkOrderVo.getSblxbm());

        Page<TEnWzCrkOrderEquipVo> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnWzCrkOrderEquipVo> iPage = tEnWzEquipMapper.selectJoinPage(page, TEnWzCrkOrderEquipVo.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return page;
    }

    @Override
    public Page<TEnWzCrkOrderEquipXqVo> pageGetEquipXq(RequestForm<TEnWzCrkOrderEquipXqVo> requestForm) {
        MPJLambdaWrapper<TEnWzCrkOrderEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.selectAll(TEnWzCrkOrderEquipEntity.class);
        queryWrapper.select(TEnWzCrkOrderEntity::getDjrq);
        queryWrapper.select(TEnWzCrkOrderEntity::getCjr);
        queryWrapper.select(TEnWzCrkOrderEntity::getShTime);
        queryWrapper.select(TEnWzCrkOrderEntity::getShPerson);
        queryWrapper.select(TEnWzEquipEntity::getSbmc);
        queryWrapper.select(TEnWzEquipEntity::getSblxbm);
        queryWrapper.select(TEnWzEquipEntity::getSblxmc);
        queryWrapper.select(TEnWzEquipEntity::getSbgg);
        queryWrapper.select(TEnWzEquipEntity::getXh);
        queryWrapper.select(TEnWzEquipEntity::getJlfsbm);
        queryWrapper.select(TEnWzEquipEntity::getJlfsmc);
        queryWrapper.select(TEnWzEquipEntity::getCcbm);
        queryWrapper.select(TEnWzEquipEntity::getCcrq);
        queryWrapper.select(TEnWzEquipEntity::getCjmc);
        queryWrapper.select(TEnWzEquipEntity::getSskfbm);
        queryWrapper.select(TEnWzEquipEntity::getSskfmc);
        queryWrapper.select(TEnWzEquipEntity::getYxzt);
        queryWrapper.select(TEnWzEquipEntity::getYxztmc);
        queryWrapper.select(TEnWzEquipEntity::getSbztbm);
        queryWrapper.select(TEnWzEquipEntity::getSbztmc);
        queryWrapper.select(TEnWzEquipEntity::getSbzp);
        queryWrapper
                .select(TEnWzEquipEntity::getGldwmc)
                .select(TEnWzEquipEntity::getGldwbm)
                .select(TEnWzEquipEntity::getPt)
                .select(TEnWzEquipEntity::getCt)
                .select(TEnWzEquipEntity::getCjtime)
                .select(TEnWzEquipEntity::getWzlbbm)
                .select(TEnWzEquipEntity::getWzlbmc)
                .select(TEnWzEquipEntity::getTyrq)
                .select(TEnWzEquipEntity::getSfjc)
                .select(TEnWzEquipEntity::getSbzb)
                .select(TEnWzEquipEntity::getSbdz)
                .select(TEnWzEquipEntity::getSftq);
        queryWrapper.innerJoin(TEnWzEquipEntity.class, TEnWzEquipEntity::getSbbm, TEnWzCrkOrderEquipEntity::getSbbm);
        queryWrapper.innerJoin(TEnWzCrkOrderEntity.class, on -> {
            on.eq(TEnWzCrkOrderEntity::getId, TEnWzCrkOrderEquipEntity::getDjbm);
            on.eq(TEnWzCrkOrderEntity::getSfsc, 0);
            return on;
        });
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getSfsc, 0);
        //queryWrapper.in(TEnWzCrkOrderEquipEntity::getSfsc,"0","1");
        queryWrapper.orderByDesc(TEnWzCrkOrderEntity::getShTime);

        if (requestForm.getEntity().getType() != null && !"".equals(requestForm.getEntity().getType())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getType, requestForm.getEntity().getType());
        }
        if (requestForm.getEntity().getSsdwbm() != null && !"".equals(requestForm.getEntity().getSsdwbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getSsdwbm, requestForm.getEntity().getSsdwbm());
        }
        if (requestForm.getEntity().getDjbm() != null && !"".equals(requestForm.getEntity().getDjbm())) {
            queryWrapper.like(TEnWzCrkOrderEquipEntity::getDjbm, requestForm.getEntity().getDjbm());
        }
        if (requestForm.getEntity().getGldwbm() != null && !"".equals(requestForm.getEntity().getGldwbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getGldwbm, requestForm.getEntity().getGldwbm());
        }
        if (requestForm.getEntity().getSbztbm() != null && !"".equals(requestForm.getEntity().getSbztbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSbztbm, requestForm.getEntity().getSbztbm());
        }
        if (requestForm.getEntity().getSblxbm() != null && !"".equals(requestForm.getEntity().getSblxbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSblxbm, requestForm.getEntity().getSblxbm());
        }
        if (requestForm.getEntity().getSskfbm() != null && !"".equals(requestForm.getEntity().getSskfbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSskfbm, requestForm.getEntity().getSskfbm());
        }
        if (requestForm.getEntity().getSbmc() != null && !"".equals(requestForm.getEntity().getSbmc())) {
            queryWrapper.like(TEnWzEquipEntity::getSbmc, requestForm.getEntity().getSbmc());
        }
        if (requestForm.getEntity().getCcbm() != null && !"".equals(requestForm.getEntity().getCcbm())) {
            queryWrapper.like(TEnWzEquipEntity::getCcbm, requestForm.getEntity().getCcbm());
        }
        if (requestForm.getEntity().getSskfmc() != null && !"".equals(requestForm.getEntity().getSskfmc())) {
            queryWrapper.eq(TEnWzEquipEntity::getSskfmc, requestForm.getEntity().getSskfmc());
        }
        if (requestForm.getEntity().getDjrq() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq());
        }
        if (requestForm.getEntity().getDjrq_to() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.le(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq_to());
        }
        if (requestForm.getEntity().getCjr() != null && !"".equals(requestForm.getEntity().getCjr())) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.like(TEnWzCrkOrderEntity::getCjr, requestForm.getEntity().getCjr());
        }

        Page<TEnWzCrkOrderEquipXqVo> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        IPage<TEnWzCrkOrderEquipXqVo> iPage = baseMapper.selectJoinPage(page, TEnWzCrkOrderEquipXqVo.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return page;
    }


    @SneakyThrows
    @Override
    public void exportExcel(HttpServletResponse response, RequestForm<TEnWzCrkOrderEquipXqVo> requestForm) {
        //设置信息头，告诉浏览器内容为excel类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //文件名称
        String fileName = "出入库设备信息.xls";
        //sheet名称
        String sheetName = "出入库设备列表";
        //设置下载名称
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        List<EnWzCrkOrderEquipExportVo> crlEquipList = this.GetEquipCrkList(requestForm);

        ExcelUtil<EnWzCrkOrderEquipExportVo> util = new ExcelUtil<>(EnWzCrkOrderEquipExportVo.class);
        util.exportExcel(response, crlEquipList, sheetName);
    }

    public List<EnWzCrkOrderEquipExportVo> GetEquipCrkList(RequestForm<TEnWzCrkOrderEquipXqVo> requestForm) {
        MPJLambdaWrapper<TEnWzCrkOrderEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzCrkOrderEquipEntity.class);
        queryWrapper.select(TEnWzCrkOrderEntity::getDjrq);
        queryWrapper.select(TEnWzCrkOrderEntity::getCjr);
        queryWrapper.select(TEnWzCrkOrderEntity::getShTime);
        queryWrapper.select(TEnWzCrkOrderEntity::getShPerson);
        queryWrapper.select(TEnWzEquipEntity::getSbmc);
        queryWrapper.select(TEnWzEquipEntity::getSblxbm);
        queryWrapper.select(TEnWzEquipEntity::getSblxmc);
        queryWrapper.select(TEnWzEquipEntity::getSbgg);
        queryWrapper.select(TEnWzEquipEntity::getXh);
        queryWrapper.select(TEnWzEquipEntity::getJlfsbm);
        queryWrapper.select(TEnWzEquipEntity::getJlfsmc);
        queryWrapper.select(TEnWzEquipEntity::getCcbm);
        queryWrapper.select(TEnWzEquipEntity::getCcrq);
        queryWrapper.select(TEnWzEquipEntity::getCjmc);
        queryWrapper.select(TEnWzEquipEntity::getSskfbm);
        queryWrapper.select(TEnWzEquipEntity::getSskfmc);
        queryWrapper.select(TEnWzEquipEntity::getYxzt);
        queryWrapper.select(TEnWzEquipEntity::getYxztmc);
        queryWrapper.select(TEnWzEquipEntity::getSbztbm);
        queryWrapper.select(TEnWzEquipEntity::getSbztmc);
        queryWrapper.select(TEnWzEquipEntity::getSbzp);
        queryWrapper
                .select(TEnWzEquipEntity::getGldwmc)
                .select(TEnWzEquipEntity::getGldwbm)
                .select(TEnWzEquipEntity::getPt)
                .select(TEnWzEquipEntity::getCt)
                .select(TEnWzEquipEntity::getCjtime)
                .select(TEnWzEquipEntity::getWzlbbm)
                .select(TEnWzEquipEntity::getWzlbmc)
                .select(TEnWzEquipEntity::getTyrq)
                .select(TEnWzEquipEntity::getSfjc)
                .select(TEnWzEquipEntity::getSbzb)
                .select(TEnWzEquipEntity::getSbdz)
                .select(TEnWzEquipEntity::getSftq);
        queryWrapper.innerJoin(TEnWzEquipEntity.class, TEnWzEquipEntity::getSbbm, TEnWzCrkOrderEquipEntity::getSbbm);
        queryWrapper.innerJoin(TEnWzCrkOrderEntity.class, on -> {
            on.eq(TEnWzCrkOrderEntity::getId, TEnWzCrkOrderEquipEntity::getDjbm);
            on.eq(TEnWzCrkOrderEntity::getSfsc, 0);
            return on;
        });
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getSfsc, 0);
        //queryWrapper.in(TEnWzCrkOrderEquipEntity::getSfsc,"0","1");
        queryWrapper.orderByDesc(TEnWzCrkOrderEntity::getShTime);

        if (requestForm.getEntity().getType() != null && !"".equals(requestForm.getEntity().getType())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getType, requestForm.getEntity().getType());
        }
        if (requestForm.getEntity().getSsdwbm() != null && !"".equals(requestForm.getEntity().getSsdwbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getSsdwbm, requestForm.getEntity().getSsdwbm());
        }
        if (requestForm.getEntity().getDjbm() != null && !"".equals(requestForm.getEntity().getDjbm())) {
            queryWrapper.like(TEnWzCrkOrderEquipEntity::getDjbm, requestForm.getEntity().getDjbm());
        }
        if (requestForm.getEntity().getGldwbm() != null && !"".equals(requestForm.getEntity().getGldwbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getGldwbm, requestForm.getEntity().getGldwbm());
        }
        if (requestForm.getEntity().getSbztbm() != null && !"".equals(requestForm.getEntity().getSbztbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSbztbm, requestForm.getEntity().getSbztbm());
        }
        if (requestForm.getEntity().getSblxbm() != null && !"".equals(requestForm.getEntity().getSblxbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSblxbm, requestForm.getEntity().getSblxbm());
        }
        if (requestForm.getEntity().getSskfbm() != null && !"".equals(requestForm.getEntity().getSskfbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSskfbm, requestForm.getEntity().getSskfbm());
        }
        if (requestForm.getEntity().getSbmc() != null && !"".equals(requestForm.getEntity().getSbmc())) {
            queryWrapper.like(TEnWzEquipEntity::getSbmc, requestForm.getEntity().getSbmc());
        }
        if (requestForm.getEntity().getCcbm() != null && !"".equals(requestForm.getEntity().getCcbm())) {
            queryWrapper.like(TEnWzEquipEntity::getCcbm, requestForm.getEntity().getCcbm());
        }
        if (requestForm.getEntity().getSskfmc() != null && !"".equals(requestForm.getEntity().getSskfmc())) {
            queryWrapper.eq(TEnWzEquipEntity::getSskfmc, requestForm.getEntity().getSskfmc());
        }
        if (requestForm.getEntity().getDjrq() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq());
        }
        if (requestForm.getEntity().getDjrq_to() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.le(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq_to());
        }
        if (requestForm.getEntity().getCjr() != null && !"".equals(requestForm.getEntity().getCjr())) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.like(TEnWzCrkOrderEntity::getCjr, requestForm.getEntity().getCjr());
        }
        List<EnWzCrkOrderEquipExportVo> entities = baseMapper.selectJoinList(EnWzCrkOrderEquipExportVo.class, queryWrapper);
        return entities;
    }


}
